package com.ddy.unitTest;

import com.ddy.test.singleton.SingletonTest;

/**
 * Created by ddy on 2020/4/21 9:33 AM
 */

public class SingletonUniteTest {
    public static void main(String[] args) {


        for(int i=0;i<=20;i++){
            int finalI = i;
            new Thread(() -> {
                String name = "c" + finalI;
                SingletonTest singletonTest = SingletonTest.getInstance();
        //        int getTime = (int)(Math.random() * 3000);
        //        try {
        //            System.out.println(name + "get size " + getTime + "s.");
        //            Thread.sleep(getTime);
        //        } catch (InterruptedException e) {
        //            e.printStackTrace();
        //        }
                int size = singletonTest.getSize();
                if(size>0){
                    System.out.print(name + " is coming, has " + size + " left. ");
                    int costTime = (int)(Math.random() * 3000);
                    try {
                        System.out.println("reached after " + costTime + "s.");
                        Thread.sleep(costTime);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    singletonTest.cost(name);
                }else{
                    System.out.println(name + "is coming, bus has no money left.");
                }
            }).start();
        }
    }
}
